<template>
  <div class="login-wrapper">
    <div class="regForm-container">
      <div class="title">后台管理系统</div>
      <el-form class="regForm" :rules="rules" :model="regForm">
        <el-form-item prop="username">
          <el-input
            type="text"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            v-model="regForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="regForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            type="password"
            placeholder="请确认密码"
            prefix-icon="el-icon-lock"
            v-model="regForm.repassword"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button class="regBtn" size="medium" type="primary" @click="register"
        >注册</el-button
      >
      <div class="toLogin">
        <el-link type="info" @click="$router.push('/login')">去登录</el-link>
      </div>
    </div>
  </div>
</template>

<script>
import { registerApi } from "@/api/login";
export default {
  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("用户名不能为空"));
      } else {
        let validMatch = value.match(/[A-Za-z0-9]+/);
        if (validMatch) {
          callback();
        } else {
          callback(new Error("用户名必须是1-10的字母数字"));
        }
      }
    };
    var validRepassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次确认密码"));
      } else {
        if (this.regForm.password !== value) {
          callback(new Error("两次密码不一致！"));
        }
      }
    };
    return {
      regForm: {
        username: "admin331",
        password: "admin123",
        repassword: "admin123",
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "用户名必须是1-10的字母数字",
            trigger: "blur",
          },
          { validator: validateUsername },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "密码必须是6-15的非空字符",
            trigger: "blur",
          },
        ],
        repassword: [
          { required: true, message: "请再次确认密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "密码必须是6-15的非空字符",
            trigger: "blur",
          },
          { validator: validRepassword },
        ],
      },
    };
  },
  created() {},
  methods: {
    async register() {
      const res = await registerApi(this.regForm);
      // console.log(res.data);
      if (res.data.code === 0) {
        this.$router.push("/login");
        this.$message.success("注册成功，请登录！");
      } else {
        this.$message.error("用户名被占用，请更换其他用户名！");
      }
    },
  },
};
</script>

<style lang="less">
.login-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
  background: #fff url("../../assets/img/login_bg.6243072a.jpg") no-repeat;
  background-size: cover;
}
.regForm-container {
  width: 340px;
  background-color: #fff;
  padding: 10px 30px 4px;

  .title {
    font-size: 24px;
    color: #19b9e7;
    padding-bottom: 20px;
    text-align: center;
  }
  .regForm {
    input {
      width: 100%;
    }
  }
  .regBtn {
    width: 100%;
  }
  .toLogin {
    line-height: 3;
    ::v-deep a {
      color: #a6a9ad;
    }
  }
}
</style>